﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using ALiteTeam.PackageTracer.Core.Impl;
using ALiteTeam.PackageTracer.Data;
using ALiteTeam.PackageTracer.ViewModel;
using ALiteTeam.PackageTracer.Core;
using Microsoft.Phone.Shell;
using GalaSoft.MvvmLight.Messaging;
using System.Windows.Navigation;

namespace ALiteTeam.PackageTracer.View
{
    public partial class MainPage : PhoneApplicationPage
    {
        private ApplicationBarIconButton _AppBarBtnRefresh;
        private ApplicationBarIconButton _AppBarBtnSettig;

        // Constructor
        public MainPage()
        {
            InitializeComponent();

            this.InitializeAppBar();
        }

        private void InitializeAppBar()
        {
            // AppBarBtnRefresh
            _AppBarBtnRefresh = new ApplicationBarIconButton(App.Current.GetIconUri(AppBarIcons.Refresh));
            _AppBarBtnRefresh.Text = "刷新";
            _AppBarBtnRefresh.Click += (btn, args) =>
                {
                    if (this.multiList.SelectedItems.Count == 0)
                    {
                        if (MessageBox.Show("没有选定任何订单，是否刷新所有运单？", "提示", MessageBoxButton.OKCancel) == MessageBoxResult.OK)
                        {
                            foreach (DeliveryOrderSubViewModel orderVM in this.multiList.ItemsSource)
                            {
                                orderVM.Initialize();
                            }
                        }
                    }
                    else
                    {
                        foreach (DeliveryOrderSubViewModel orderVM in this.multiList.SelectedItems)
                        {
                            orderVM.Initialize();
                        }
                    }
                };

            // AppBarBtnSetting
            _AppBarBtnSettig = new ApplicationBarIconButton(App.Current.GetIconUri(AppBarIcons.Settings));
            _AppBarBtnSettig.Text = "设置";
            _AppBarBtnSettig.Click += (btn, args) =>
            {
                NavigationService.Navigate(new Uri("/View/SettingPage.xaml", UriKind.Relative));
            };

            // 如果"设置"AppBar按钮不存在，则添加
            if (this.ApplicationBar.Buttons.Contains(this._AppBarBtnSettig) == false)
            {
                this.ApplicationBar.Buttons.Add(this._AppBarBtnSettig);
            }
        }

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            string index = string.Empty;
            if (NavigationContext.QueryString.Keys.Contains("Index") == true)
            {
                index = NavigationContext.QueryString["Index"];
            }

            var message = new NotificationMessage<NavigationEventArgs>(e, index);
            Messenger.Default.Send<NotificationMessage<NavigationEventArgs>>(message, MainPageViewModel.ON_NAVIGATED_TO);

            base.OnNavigatedTo(e);
        }

        private void pivot_LoadingPivotItem(object sender, PivotItemEventArgs e)
        {
            switch (e.Item.Name)
            {
                    // 如果是选择快递Tab
                case "pivotItemChooseDelivery":
                    {
                        // 如果"刷新"AppBar按钮存在，则移除
                        if (this.ApplicationBar.Buttons.Contains(this._AppBarBtnRefresh) == true)
                        {
                            this.ApplicationBar.Buttons.Remove(this._AppBarBtnRefresh);
                        }
                    }
                    break;
                case "pivotItemMyOrders":
                    {
                        // 如果"刷新"AppBar按钮不存在，则添加
                        if (this.ApplicationBar.Buttons.Contains(this._AppBarBtnRefresh) == false)
                        {
                            this.ApplicationBar.Buttons.Insert(0, this._AppBarBtnRefresh);
                        }
                    }
                    break;
            }
        }
    }
}